package com.alibaba.product.controller;

import com.alibaba.basic.jwt.UserInfo;
import com.alibaba.basic.uitl.AjaxResult;
import com.alibaba.basic.uitl.LoginContext;
import com.alibaba.basic.uitl.PageList;
import com.alibaba.org.domain.Employee;
import com.alibaba.org.mapper.EmployeeMapper;
import com.alibaba.product.domain.ProductMsg;
import com.alibaba.product.dto.PorductMsgDto;
import com.alibaba.product.query.ProductMsgQuery;
import com.alibaba.product.service.IProductMsgService;
import com.alibaba.user.domain.Logininfo;
import com.alibaba.user.domain.User;
import com.alibaba.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/productMsg")
public class ProductMsgController {
    @Autowired
    public IProductMsgService productMsgService;
    @Autowired
    public IUserService userService;
    @Autowired
    public EmployeeMapper employeeMapper;

    /**
     * 保存和修改公用的
     * @param productMsg  传递的实体
     * @return AjaxResult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody ProductMsg productMsg){
        try {
            if( productMsg.getId()!=null)
                productMsgService.update(productMsg);
            else
                productMsgService.add(productMsg);
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            productMsgService.delete(id);
            return AjaxResult.me().success();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public ProductMsg get(@PathVariable("id")Long id)
    {
        return productMsgService.finById(id);
    }


    /**
     * 前台分页查询数据
     * 这个接口是用户在个人中心查看自己发布的所有消息
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping("/front/list")
    public PageList<ProductMsg> frontList(@RequestBody ProductMsgQuery query)
    {
        UserInfo userInfo = LoginContext.getUserInfo();
        User user = userService.findByLogininfoId(userInfo.getLogininfo().getId());
        query.setUserId(user.getId());
        return productMsgService.queryByPage(query);
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<ProductMsg> json(@RequestBody ProductMsgQuery query)
    {
        return productMsgService.queryByPage(query);
    }

    /**
     * 提交订单
     *
     * @param
     * @return PageList 分页对象
     */
    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody @Valid PorductMsgDto dto)
    {
        productMsgService.publish(dto);
        return AjaxResult.me().success();
    }

    @PostMapping("/tohandle")
    public PageList<ProductMsg> tohandle(@RequestBody ProductMsgQuery query)
    {
        // 获取当前店铺id 审核成功 店铺id=shopId
        Logininfo logininfo = LoginContext.getLogininfo();
        Employee byLogininfoId = employeeMapper.findByLogininfoId(logininfo.getId());
        query.setShopId(byLogininfoId.getShopId());
        query.setState(1);
        return productMsgService.queryByPage(query);
    }

    /**
     * 拒单
     * @param query
     * @return
     */

    @PostMapping("/reject/{id}")
    public AjaxResult reject(@PathVariable Long id) {
        productMsgService.reject(id);
        return AjaxResult.me().success();

    }

}
